import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * author: MaHX
 * date: 2019/9/19/15:30
 * description:
 **/
public class TestABCAlternate {
    public static void main(String args[]) {
        final AlternateDemo alternateDemo = new AlternateDemo();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    alternateDemo.loopA(i);
                }
            }
        },"A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    alternateDemo.loopB(i);
                }
            }
        },"B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    alternateDemo.loopC(i);
                }
            }
        },"C").start();

    }

    static class AlternateDemo {
        private int number = 1;   //作为线程的标志
        private Lock lock = new ReentrantLock();
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();

        public void loopA(int total) {
            lock.lock();
            try {
                if (number != 1) {
                    try {
                        condition1.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i +"\t" + total);
                }
                number = 2;
                condition2.signal();

            } finally {
                lock.unlock();
            }
        }
        public void loopB(int total) {
            lock.lock();
            try {
                if (number != 2) {
                    try {
                        condition2.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i +"\t" + total);
                }
                number = 3;
                condition3.signal();

            } finally {
                lock.unlock();
            }
        }
        public void loopC(int total) {
            lock.lock();
            try {
                if (number != 3) {
                    try {
                        condition3.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i +"\t" + total);
                }
                System.out.println("-----------------");
                number = 1;
                condition1.signal();

            } finally {
                lock.unlock();
            }
        }
    }
}
